<template>
  <div>
    <TopFilter ref="formpanel1" :list="formList1" labelWidth="100" :collapse="true" :isSubmitBtn="true" @filterChange="formchangeFun" />
    <FilterTable
      ref="basetable"
      :columns="columns"
      columnsRef="partAccessAccessInventoryAddNewPart"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      maxHeight="350"
      :onEnterEvent="tableDbclickFun"
      selectionType="single"
      :isPagination="true"
      :onRowSelectChange="selectTableFun"
    ></FilterTable>
    <!-- :dataSource="toptablelist" -->
    <div class="btnWrap">
      <el-button @click="closeDialog">关闭</el-button>
      <!-- <el-button type="primary" @click="confirmHandle">保存</el-button> -->
    </div>
  </div>
</template>
<script>
import axios from '@/api/fetch';
import SERVER from '@/api/server';
import { getaddPartList } from '@/api/part/partInventory/adjustInventory/index';
import { getPosition, getfindStorageCode } from '@/api/part/inventoryManager/inventoryManager/index';
import { queryBrand } from '@/api/part/partOut/partSales/index';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { findAllRepair } from '@/api/repair/basicParameters';
import { dictionary } from '@/utils/dictMixin';
import { getNewPartData, checkPart } from '@/api/part/access/accessInventory';
import { getBrand } from '@/api/repair/basicParameters';
import { notifyAction } from '../../../../utils';
export default {
  components: {},
  mixins: [dictionary],
  data() {
    this.basetable = null;
    return {
      formData: {},
      pdiShow: false, // 控制弹框
      formList1: this.createFrom(),
      fetchApi: getNewPartData,
      fetchParams: {
        xhrAbort: true,
        sort: '',
        order: 'asc',
        // limit: '10',
        // offset: '0',
        queryPart_table: ''
      },
      columns: this.createTable(),
      selectPartRow: {},
      emptyTableData: [], // 用于清空表格数据
      toptablelist: [],
      tableslist: [],
      storageList: [],
      fromSubmitdata: {},
      replacedata: {},
      // 仓库代码列表
      storageCodeList: [],
      // 名字
      storageNameList: []
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.basetable = this.$refs.basetable.$refs.pageTable;
  },
  methods: {
    // 行双击事件
    async tableDbclickFun(val) {
      val.CURRENT_STOCK = this.selectPartRow[0].STOCK_QUANTITY;
      val.CHECK_QUANTITY = this.selectPartRow[0].STOCK_QUANTITY;
      let res = await checkPart(val);
      if (res.resultCode == 200 && res.data == false) {
        this.$notify({ title: '警告信息', message: '该配件存在未入账的盘点单', type: 'warning' });
        return;
      }
      this.$emit('confirm', val);
    },
    // 弹框确定
    showOrderDetail(val) {
      this.pdiShow = false;
      this.$emit('addNewPartnew', val);
    },
    // 弹框确定
    closeHandler() {
      this.pdiShow = false;
    },
    createFrom() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'storageCode',
          // rules: [{ required: true, message: '请输入仓库', trigger: 'blur' }],
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '品牌',
        //   fieldName: 'brand',
        //   itemList: []
        // },
        // {
        //   type: 'SELECT',
        //   label: '配件类别',
        //   fieldName: 'partGroupCode',
        //   itemList: this.createDictList('8247')
        // },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        // {
        //   type: 'INPUT',
        //   label: '拼音',
        //   fieldName: 'spellCode'
        // },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        },
        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode'
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark'
        }
      ];
    },
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库',
          dataIndex: 'STORAGE_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props => {
          //   return <span>{this.getStroageName(props.row.STORAGE_CODE)}</span>;
          // }
        },
        {
          title: '库位',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.STORAGE_POSITION_CODE}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.PART_NO}</span>;
          }
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.PART_NAME}</span>;
          }
        },
        // {
        //   title: '含税销售单价',
        //   dataIndex: 'SALES_PRICE',
        //   sorter: true,
        //   filter: true,
        //   precision: 2,
        //   filterType: 'number',
        //   render: props => {
        //     return <span>{props.row.SALES_PRICE ? props.row.SALES_PRICE.toFixed(2) : 0}</span>;
        //   }
        // },
        {
          title: '含税成本单价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'number',
          render: props => {
            return <span>{props.row.COST_PRICE ? props.row.COST_PRICE.toFixed(2) : 0}</span>;
          }
        },
        // {
        //   title: '可用库存',
        //   dataIndex: 'USEABLE_STOCK',
        //   sorter: true,
        //   filter: true,
        //   precision: 2,
        //   filterType: 'number',
        //   render: props => {
        //     return <span>{props.row.USEABLE_STOCK ? props.row.USEABLE_STOCK.toFixed(2) : 0}</span>;
        //   }
        // },
        // {
        //   title: '账面库存',
        //   dataIndex: 'STOCK_QUANTITY',
        //   sorter: true,
        //   filter: true,
        //   precision: 2,
        //   filterType: 'number',
        //   render: props => {
        //     return <span>{props.row.STOCK_QUANTITY ? props.row.STOCK_QUANTITY.toFixed(2) : 0}</span>;
        //   }
        // },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.REMARK}</span>;
          }
        }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      let res = await getStroageList();
      // const resd = await queryBrand();
      const resd = await getBrand();
      let resdata = await findAllRepair();
      this.storageList = res.data.map(x => ({ value: x.STORAGE_CODE, text: x.STORAGE_NAME }));
      // this.formList1.find(item => item.fieldName === 'brand').itemList = resd.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_CODE + '' }));
      this.formList1.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      // this.formList1.find(item => item.fieldName === 'storageCode').initialValue = resdata.data['1100'];
    },
    getStroageName(STORAGE_CODE) {
      const item = this.storageList.find(x => {
        return x.value == STORAGE_CODE;
      });
      return item ? item.text : '';
    },
    // 重置
    FormPanelreset() {
      this.$refs.formpanel1.RESET_FORM();
    },
    FormPanelSeacher() {
      this.$refs.formpanel1.SUBMIT_FORM();
      this.$refs.formpanel2.RESET_FORM();
      this.emptyTableData = [];
    },
    // 选中事件
    selectTableFun(val) {
      this.selectPartRow = val;
    },
    formchangeFun(val) {
      this.fetchParams.xhrAbort = true;
      // console.log(val, 12321313);

      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    // 查询新增配件信息
    // async getNewPartData(params) {
    //   const res = await axios.get(`${SERVER.DMSCLOUD_PART}/stockmanage/allocateInOders/findPartForAdd`, { params });
    //   return res;
    // },
    async getpartlist(val) {
      this.emptyTableData = [];
      let res = (await getaddPartList(val.STORAGE_CODE, val.PART_NO)).data.rows;
      this.basetables.EXECUTE_INSERT(res);
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('cancel', false);
    },
    tablechangeFun(val) {
      this.tableslist = val;
    },
    // 添加事件提交表单
    confirmHandle() {
      if (this.selectPartRow && this.selectPartRow.length > 0) {
        this.selectPartRow[0].CURRENT_STOCK = this.selectPartRow[0].STOCK_QUANTITY;
        this.selectPartRow[0].CHECK_QUANTITY = this.selectPartRow[0].STOCK_QUANTITY;
        this.$emit('confirm', this.selectPartRow[0]);
      } else {
        return this.$notify({ title: '警告信息', message: '请选择要添加的配件！', type: 'warning' });
      }
    }
  }
};
</script>
<style>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
